package be.kuleuven.peno3.mobiletoledo.view.multiplechoice;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import be.kuleuven.peno3.mobiletoledo.R;
import be.kuleuven.peno3.mobiletoledo.Data.Client.MultipleChoiceClient;
import be.kuleuven.peno3.mobiletoledo.model.Question;
import be.kuleuven.peno3.mobiletoledo.model.User;

public class ManageQuestions extends Activity {
	private HashMap<String, Integer> questionids;
	private LinearLayout ll;
	
	private int course_id;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		questionids=new HashMap<String, Integer>();
		
		course_id=this.getIntent().getExtras().getInt("course_id");
		
		String[] questions = fetchQuestions();
		
		if(questions != null) {
			displayQuestions(questions);
		}
	}
	
	@Override
	public void onResume() { // Als men een vraag heeft toegevoegd of beantwoord, en men keert terug naar deze Activity, moet de lijst met vragen geupdated worden.
		super.onResume();
		String[] questions = fetchQuestions();
		if(questions != null) {
			displayQuestions(questions);
		}
	}
	
	/**
	 * Lijst met vragen ophalen uit de database
	 * @return String[] met daarin alle vragen
	 */
	private String[] fetchQuestions() {
		System.out.println("test " +course_id);
		Question[] q = MultipleChoiceClient.getQuestions(course_id);
		
		if(q==null) {
			Toast.makeText(this.getApplicationContext(), "Er is geen internetverbinding", Toast.LENGTH_SHORT).show();
			finish();
			return null;
		}
		String[] questions;
		
		if(User.getInstance().isTeacher()) {
			questions = new String[q.length+1];
			questions[0]=getString(R.string.addanewquestion);
		}
		else {
			questions = new String[q.length];
		}
		
		for(int i=0;i<q.length;i++) {
			if(User.getInstance().isTeacher()) {
				questions[i+1] = q[i].getContent();
			}
			else {
				questions[i] = q[i].getContent();
			}
			questionids.put(q[i].getContent(), q[i].getId()); // we houden lokaal ook de id's van de vragen bij, die we nodig hebben om vragen te bekijken/beantwoorden
		}
		return questions;
	}
	
	private void displayQuestions(String[] questions) {
		setContentView(R.layout.textlistlayout);
		
		TextView manQuestBar = (TextView) findViewById(R.id.top_bar);
		manQuestBar.setText("Poll");
		ll = (LinearLayout) findViewById(R.id.man_quest_ll);
		
		for (int i = 0; i < questions.length; i++){
			if(i > 0){
				ImageView image = new ImageView(this.getApplicationContext());
				image.setImageDrawable(getResources().getDrawable(R.drawable.line));
				ll.addView(image);
			}
			
			TextView text = new TextView(this.getApplicationContext());
			text.setText(questions[i]);
			text.setClickable(true);
			if(i==0 && User.getInstance().isTeacher()){
				text.setTextSize(TypedValue.COMPLEX_UNIT_PT, 12);
				text.setTextColor(0xFF69931e);
			}
			else{
				text.setTextSize(TypedValue.COMPLEX_UNIT_PT, 10);
			}
			text.setGravity(Gravity.CENTER_HORIZONTAL);
			text.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					whenClicked(v);
				}
			}
			);
			ll.addView(text);
		}
	}
	
	/**
	 * Called when the user clicks on a menu-item, and opens the correct Activity
	 * @param view The view that was clicked
	 */
	public void whenClicked(View view) {
		TextView v=(TextView) view;
		onClickAnimation(v);
		if(v.getText().equals(getString(R.string.addanewquestion))) {
			Intent i=new Intent(this, AddQuestion.class);
			i.putExtra("course_id", course_id);
			activityStartDelay(i);
		}
		else {
			String question = v.getText().toString();
			int questionId=questionids.get(question);
			Intent i;
			if(User.getInstance().isTeacher()) {
				i = new Intent(this, QuestionResult.class);
			}
			else {
				i = new Intent(this, AnswerQuestion.class);
			}
			i.putExtra("questionId", questionId);
			activityStartDelay(i);
		}
	}
	
	private void onClickAnimation(View view){
		view.startAnimation(AnimationUtils.loadAnimation(this, R.layout.image_click));
	}
	
	private void activityStartDelay(Intent intent){
		int delay = 400; //Time to delay activity startup
		final Intent i = intent;
		final Handler handler = new Handler(); 
        Timer t = new Timer(); 
        t.schedule(new TimerTask() { 
        	public void run() { 
        		handler.post(new Runnable() { 
        			public void run() { 
        				startActivity(i); 
        			} 
        		}); 
        	} 
        }, delay);
	}
}
